<template>
  <a-drawer
    :title="formInfo.id == undefined ? '新增' : '修改'"
    :maskClosable="true"
    width="400"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :destroyOnClose="true"
    :visible="visible"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
        <a-row>
          <a-col>
            <a-form-model-item
              label="标准品牌"
              prop="labelBrandName"
              :rules="$valid.required"
              :maxLength="50"
            >
              <a-input
                v-model="formInfo.labelBrandName"
                placeholder="请输入标准品牌"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col>
            <a-form-model-item
              label="关联机构"
              prop="relatedInstitutions"
              :maxLength="50"
            >
              <a-input
                v-model="formInfo.relatedInstitutions"
                placeholder="请输入关联机构"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <div
          style="
            display: flex;
            justify-content: flex-end; /* 将内容推到右侧 */
            align-items: flex-end; /* 将内容推到下方 */
            height: 1000vh; /* 容器高度设为视口高度，确保它在屏幕底部 */
            margin-top: 115%;
          "
        >
          <a-button @click="handleCancel" style="margin-right: 0.8rem"
            >取消</a-button
          >
          <a-button
            style="margin-right: 0.8rem"
            @click="handleSubmit"
            type="primary"
            :loading="confirmLoading"
            >保存</a-button
          >
        </div>
      </a-form-model>
    </a-spin>
  </a-drawer>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
export default {
  name: 'DevArchiveLabelBrandEdit',
  components: {},
  data() {
    return {
      visible: false,
      confirmLoading: false,
      formInfo: {
        createTime: undefined,
        labelBrandName: undefined,
        relatedInstitutions: undefined,
        wx: '',
      },
      url: {
        saveInfo: '/system/devArchiveLabelBrand/save',
        getInfo: '/system/devArchiveLabelBrand/info',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit()
    },
    edit(record) {
      this.$objToOverride(this.formInfo, record)
      if (this.formInfo.id) {
        getAction(this.url.getInfo, { id: record.id }).then((res) => {
          this.$nextTick(() => {
            this.formInfo = res.result.data
          })
        })
      }
      this.visible = true
    },
    // 确定
    handleSubmit() {
      // 触发表单验证
      this.confirmLoading = true
      this.$refs.ruleForm.validate((valid) => {
        console.log(valid)
        if (valid) {
          postAction(this.url.saveInfo, this.formInfo)
            .then((res) => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$refs.ruleForm.resetFields()
      this.$objToReset(this.formInfo)
      this.$emit('close')
      this.visible = false
    },
    editorOk(val) {
      this.formInfo.content = val
    },
  },
}
</script>
